package com.ayic.garbagerecycle.interceptor;

import com.ayic.garbagerecycle.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component  //交付IOC 管理
public class TokenInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

//        String URI = request.getRequestURI();
//
//        if ("OPTIONS".equals(request.getMethod())) {
//            return true;
//        }
//
//        if (URI.contains("/login")) {
//            log.info("登录请求，默认放行");
//            return true;
//        }
//        System.out.println(request);
//        String token = request.getHeader("Authorization");
//        if (token == null) {
//            log.info("令牌不存在，Code：401");
////          response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//            return true;
//        }
//
//        try {
//            JwtUtils.parseToken(token.substring(7));
//        } catch (Exception e) {
//            log.info("令牌非法");
//            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//            return false;
//        }

        log.info("令牌合法，放行");
        return true;
    }
}
